package lomasky.xui;

import android.content.Context;


import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

import com.bumptech.glide.Glide;

import java.util.ArrayList;
import java.util.List;


public class ImgShower extends GridView {

    Context content;
    public void setShowCamera(boolean showCamera) {
        this.showCamera = showCamera;
        adapter.notifyDataSetChanged();
    }

    boolean showCamera = true;
    public ImgShower(Context context) {
        super(context);
        this.content = context;
        init(context);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
                MeasureSpec.AT_MOST);
        super.onMeasure(widthMeasureSpec, expandSpec);
    }

    public void setList(List<String> list) {
        adapter.clear();
        adapter.data .addAll(list);
        adapter.notifyDataSetChanged();
        requestLayout();

    }


    public List<String> getList() {
        List<String> list = new ArrayList<>();
        for (Object o : adapter.getData()) {
            if (o instanceof String) {
                list.add(o.toString());
            }
        }
        return list;
    }


    PhotoAdapter adapter;

    private void init(Context context) {
        setNumColumns(3);
        adapter = new PhotoAdapter();
        setAdapter(adapter);

    }


    public ImgShower(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);

    }

    public class PhotoAdapter extends BaseAdapter {
        private static final int TYPE_CAMERA = 1;
        private static final int TYPE_NORMAL = 2;

        public PhotoAdapter() {
            inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }
        public List<String> getData() {
            return data;
        }

        List<String> data = new ArrayList<>();

        public void clear() {
            data.clear();
            notifyDataSetChanged();
        }

        public void addAll(List<String> items) {
            if (items.isEmpty()) {
                return;
            }

            data.addAll(items);
            notifyDataSetChanged();
        }

        public void onBindViewHolder(PhotoAdapter.PhotoViewHolder holder, int position) {
            if (getItemViewType(position) == TYPE_CAMERA) {
                Glide.with(getContext())
                        .load(R.drawable.ic_add_48dp)
//                       .placeholder(R.drawable.pictures_no)
//                       .error(R.drawable.pictures_no)
                        .override(200, 200)
                        .centerCrop()
                        .into(holder.imageView);
            } else {
                Glide.with(getContext())
                        .load(data.get(position-1))
                        .placeholder(R.drawable.pictures_no)
                        .error(R.drawable.pictures_no)
                        .override(200, 200)
                        .centerCrop()
                        .into(holder.imageView);
            }


        }

        @Override
        public int getItemViewType(int position) {
            if (showCamera) {
                return position == 0 ? TYPE_CAMERA : TYPE_NORMAL;
            }
            return TYPE_NORMAL;
        }

        @Override
        public int getCount() {
            if (showCamera) {
                return data.size()+1;
            } else {
                return data.size();
            }


        }

        public String getItem(int i) {
            if (showCamera) {
                if (i == 0) {
                    return null;
                }
                return data.get(i - 1);
            } else {
                return data.get(i);
            }
        }

        @Override
        public long getItemId(int i) {
            return i;
        }


        private LayoutInflater inflater;

        @Override
        public View getView(int i, View convertView, ViewGroup viewGroup) {

            PhotoViewHolder holder;
            if (convertView == null) {
                holder = new PhotoViewHolder();
                convertView = inflater.inflate(R.layout.simple_img_item, null);
                holder.imageView = (ImageView) convertView.findViewById(R.id.img);
                convertView.setTag(holder);
            } else {
                holder = (PhotoViewHolder) convertView.getTag();
            }

            onBindViewHolder(holder, i);
            return convertView;

        }

        class PhotoViewHolder {
            public ImageView imageView;

        }
    }


}
